package io.renren.modules.shop.util;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import io.renren.modules.shop.entity.admin.Admin;
import io.renren.modules.shop.entity.user.User;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.apache.shiro.subject.Subject;

/**从request，session中获取信息*/
public class SessionUtil {
	/**从request中获取后端用户
	 * @param request request
	 * 登入后存入session的对象*/
	public static Admin getAdminUser(HttpServletRequest request) {

		HttpSession session = request.getSession();
		System.out.println("get时的sessionid为:" + session.getId());

		return (Admin) request.getSession().getAttribute("adminUser");
	}

	//		HttpSession session = request.getSession(true);
//		return (Admin)session.getAttribute("adminUser");
//		Session session = SecurityUtils.getSubject().getSession();
//		return (Admin)session.getAttribute("adminUser");


	public static void setAdminUser(HttpServletRequest request,Admin admin){
		request.getSession(true).setAttribute("adminUser",admin);


		HttpSession session = request.getSession();
		System.out.println("set时的sessionid为:" + session.getId());


	}

	//  (Admin) request.getSession(true).getAttribute("adminUser");
	//		return (Admin) request.getAttribute("adminUser");
	//		request.setAttribute("adminUser",admin);

	//		Session session = SecurityUtils.getSubject().getSession();
	//		session.setAttribute("adminUser",admin);
	//		System.out.println("set时的sessionid为:"+request.getRequestedSessionId());
	
	public static void setUser(HttpServletRequest request,User user){
//		request.getSession().setAttribute("user", user);
		request.setAttribute("user", user);
	}
	/**从session中获取前端用户
	 * @param  request
	 *  登入后存入session的对象*/
	public static User getUser(HttpServletRequest request){
//		return (User)request.getSession().getAttribute("user");
		return (User)request.getAttribute("user");
	}
	
	/**
     * 获取访问者IP
     * 在一般情况下使用Request.getRemoteAddr()即可，但是经过nginx等反向代理软件后，这个方法会失效。
     * 本方法先从Header中获取X-Real-IP，如果不存在再从X-Forwarded-For获得第一个IP(用,分割)，
     * 如果还不存在则调用Request.getRemoteAddr()。
     * @param request
     * @return
     */
	public static String getLogHead(HttpServletRequest request){
		return "ip为 "+getIpAddr(request)+" ";
	}
	public static String getIpAddr(HttpServletRequest request){
    	String ip = request.getHeader("X-Real-IP");
    	if (StringUtils.isNotEmpty(ip)&& !"unknown".equalsIgnoreCase(ip)) {
			return ip;
		}
    	ip = request.getHeader("X-Forwarded-For");
    	if (StringUtils.isNotEmpty(ip)&& !"unknown".equalsIgnoreCase(ip)) {
    		// 多次反向代理后会有多个IP值，第一个为真实IP。
    		int index = ip.indexOf(',');
    		if (index != -1) {
    			return ip.substring(0, index);
    		} else {
    			return ip;
    		}
		} else {
			return request.getRemoteAddr();
		}
    }
}
